package etc.com.market;

import java.util.ArrayList;

import etc.com.sql.DBAdapter;

import Controls.JsonDataHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ImageZoomActivity extends Activity{
	/** Called when the activity is first created. */
	ArrayList<String> arrtext = new ArrayList<String>();
	int[] idimage = new int[6];
	int[] idimage1 = new int[6];
	String prodname;
	String imagename;
	String previous;
	String storename;
	String str;
	String productId;
	int position;
	int image;
	ImageView imageview;
	private static final int REQUEST_CODE = 10;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras =  getIntent().getExtras();
		if (extras == null) {
			return;
		}
//		String messageReceive = extras.getString("message");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.image_zoom);
		
		RelativeLayout gallerylayout = (RelativeLayout) findViewById(R.id.gallery);

		productId = extras.getString("productId");
		position = extras.getInt("imageIndex");
		
		DBAdapter dbAdapter = new DBAdapter(getApplicationContext());
		Cursor cursor = dbAdapter.getProductDetailsById(productId);
		if(cursor.getCount() > 0){
			cursor.moveToNext();
			String imageListString = cursor.getString(8);
			imageListString = imageListString.substring(1, imageListString.length() - 1);
			String[] imagePaths = imageListString.split(",");
			Bitmap[] ImageList = new Bitmap[imagePaths.length];
			for(int i = 0; i < imagePaths.length; i++){
				String imagePath = imagePaths[i].trim().substring(1, imagePaths[i].trim().length() - 1);
				String imageValue = JsonDataHelper.getImageContent(imagePath);
				byte[] decodeBytes = Base64.decode(imageValue.getBytes(), Base64.DEFAULT);
				ImageList[i] = BitmapFactory.decodeByteArray(decodeBytes, 0, decodeBytes.length);
			}
			
			Bitmap objBitmap = ImageList[position];
			TouchView gallery = new TouchView(this,objBitmap);
			gallery.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
	                ViewGroup.LayoutParams.MATCH_PARENT));
			gallerylayout.addView(gallery);
		}
		cursor.close();
		
//		if (messageReceive != null) {
//			str = messageReceive.toString();
//			previous = str.substring(0, str.indexOf(";"));
//			storename = str.substring(str.indexOf(";") + 1);
//		}
//		
		TextView v = (TextView) findViewById(R.id.storename);
		v.setText(storename);
	}
	public void galleryToStoreList(View view) {
		Intent i = new Intent(this, StoreListAct.class);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	// function use for footer button ==============================
		public void Previous(View view) {
			//onClickPre(view, ("店舗詳細;" + storename));
			finish();
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}

		public void Home(View view) {
			Intent i = new Intent(this, StoreTopPage.class);
			i.putExtra("message", storename);
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}

		public void SearchProduct(View view) {
			onClickSearch(view, ("店舗詳細;" + storename));
		}

		public void SpecifyProduct(View view) {
			onClickSpec(view, ("店舗詳細;" + storename));
		}

		public void Info(View view) {
			onClickInfo(view, storename);
		}

		public void Search(View view) {
			onClickSearch(view, ("店舗詳細;" + storename));
		}
		
		public void Contact(View view) {
			onClickContact(view, ("店舗詳細;" + storename));
		}
		
		// ===================================================================

		public void onClick(View view, String message) {
			Intent i = new Intent(this, MeatAct.class);
			i.putExtra("message", message);
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}

		public void onClickPre(View view, String message) {
			Intent i = new Intent(this, MeatAct.class);
			// i.putExtra("message", message);
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}

		public void onClickSpec(View view, String message) {
			Intent i = new Intent(this, ListitemActivity.class);
			i.putExtra("message", message);
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}

		public void onClickSearch(View view, String message) {
			Intent i = new Intent(this, SearchOursProducts.class);
			i.putExtra("message", message);
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}

		public void onClickInfo(View view, String message) {
			Intent i = new Intent(this, InfomationAct.class);
			i.putExtra("message", message);
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}
		
		public void onClickContact(View view, String message) {
			Intent i = new Intent(this, SenmailActivity.class);
			i.putExtra("message", message);
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}

		public void morePictures(View view) {
			Intent i = new Intent(this, moreImage.class);
			i.putExtra("message",
					"Product Detail;" + storename + ";" + imagename.toString());
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}

		public void viewVideo(View view) {
			Intent i = new Intent(this, ViewVideo.class);
			i.putExtra("message",
					"Product Detail;" + storename + ";" + imagename.toString());
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}
}